/* 
 * Copyright IBM Japan, AMS Services 2003 ALL Rights Reserved.
 * 
 * This product includes software developed by 
 * the Apache Software Foundation (http://www.apache.org/).
 */
package com.tianqu.tms.core.mail;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataSource;


/**
 * <p>byte（二进制）数据专用的DataSource类</p><br />
 * mail发信使用的附件是byte(二进制文件)时候使用<br />
 * @author liugang
 */
public class ByteArrayDataSource implements DataSource {


/*-------------------- Fields --------------------*/

    /**
     * <p>对象名称</p>
     */
    private String name = null;

    /**
     * <p>byte数据数组</p>
     */
    private byte[] bytes = null;

    /**
     * <p>MIME类型</p>
     */
    private String contentType = null;

    /**
     * <p>ByteArrayDataSource的初始化</p>
     * 
     * @param name
     * @param bytes
     * @param contentType
     */
    public ByteArrayDataSource( String name,
                                byte[] bytes,
                                String contentType ) {
        this.name        = name;
        this.bytes       = bytes;
        this.contentType = contentType;
    }

    /**
     * <p>获得当前对象的inputstream</p>
     * 
     * @return InputStream
     * @throws IOException
     */
    public InputStream getInputStream() throws IOException {
        if ( this.bytes == null ) {
            // 没有数据时候
            throw new IOException( "no data" );
        }
        return new ByteArrayInputStream( this.bytes );
    }


    /**
     * <p>获得当前对象的outputstream</p><br />
     * 本方案并提供返回当前对象的outputstream,一调用就返回异常<br />
     * 
     * @return OutputStream 获得当前对象的outputstream（实际情况是不返回的）
     * @throws IOException 调用本方法就抛出异常
     */
    public OutputStream getOutputStream() throws IOException {
        throw new IOException( "cannot do this" );
    }

    public String getName() {
        return this.name;
    }
    public String getContentType() {
        return this.contentType;
    }
}
